package org.ktpro.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.ktpro.shortlink.project.dao.entity.ShortLinkDO;
import org.ktpro.shortlink.project.dao.mapper.ShortLinkMapper;
import org.ktpro.shortlink.project.dto.req.RecycleBinRemoveReqDTO;
import org.ktpro.shortlink.project.dto.req.RecycleBinReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkRecycleBinPageReqDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import org.ktpro.shortlink.project.service.RecycleBinService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static org.ktpro.shortlink.project.common.constant.RedisConstant.GOTO_IS_NULL_SHORT_LINK_KEY;
import static org.ktpro.shortlink.project.common.constant.RedisConstant.GOTO_SHORT_LINK_KEY;

/**
 * 回收站接口实现层
 *
 * @Author Kris20030907
 * QQ: 3185633428 微信: 18712679036
 */
@Service(value = "recycleBinServiceImplByProject")
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDO> implements RecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 短链接移进回收站
     */
    @Override
    public void saveRecycleBin(RecycleBinReqDTO requestParam) {
        //其实就是修改启用状态
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 1)   //找到已经启用的
                .eq(ShortLinkDO::getDelFlag, 0);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(0)
                .build();
        //移进回收站还要删除一下该短链接跳转的缓存，否则还会访问到
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
        baseMapper.update(shortLinkDO, updateWrapper);
    }

    /**
     * 短链接移出回收站
     */
    @Override
    public void removeRecycleBin(RecycleBinReqDTO requestParam) {
        //要做的是修改短链接状态，同时删除缓存中对该链接缓存的“空值”，否则还会导致链接不能访问
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 0)   //找未启用的
                .eq(ShortLinkDO::getDelFlag, 0);//未删除
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(shortLinkDO, updateWrapper);

        //判断是否存在空值缓存,如果有就删除
        stringRedisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public void removeFullyRecycleBin(RecycleBinRemoveReqDTO requestParam) {
        //直接找到该短链接，然后逻辑删除就行了，加入回收站肯定是没缓存的了。
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelTime, 0L)
                .eq(ShortLinkDO::getEnableStatus, 0);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .delFlag(1)
                .delTime(System.currentTimeMillis())
                .build();
        baseMapper.update(shortLinkDO, updateWrapper);
    }

    /**
     * 分页查询回收站短链接
     */
    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getEnableStatus, 0)
                .eq(ShortLinkDO::getDelFlag, 0)
                .orderByDesc(ShortLinkDO::getUpdateTime);
        IPage<ShortLinkDO> resultPage = baseMapper.selectPage(requestParam, queryWrapper);
        return resultPage.convert(each -> {
            ShortLinkPageRespDTO result = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
            result.setDomain("http://" + result.getDomain());
            return result;
        });
    }
}
